/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.mer.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.mer.entity.OperationContentDetail;
import com.kaaction.modules.mer.service.OperationContentDetailService;

/**
 * 精选商品推荐Controller
 * @author huanghaiming
 * @version 2017-07-11
 */
@Controller
@RequestMapping(value = "${adminPath}/mer/operationContentDetail")
public class OperationContentDetailController extends BaseController {

	@Autowired
	private OperationContentDetailService operationContentDetailService;
	
	@ModelAttribute
	public OperationContentDetail get(@RequestParam(required=false) String id) {
		OperationContentDetail entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = operationContentDetailService.get(id);
		}
		if (entity == null){
			entity = new OperationContentDetail();
		}
		return entity;
	}
	
	@RequiresPermissions("mer:operationContentDetail:view")
	@RequestMapping(value = {"list", ""})
	public String list(OperationContentDetail operationContentDetail, HttpServletRequest request, HttpServletResponse response, Model model) {
		operationContentDetailService.updateOpeContentTitle(operationContentDetail);
		Page<OperationContentDetail> page = operationContentDetailService.findPage(new Page<OperationContentDetail>(request, response), operationContentDetail); 
		model.addAttribute("page", page);
		return "modules/mer/operationContentDetailList";
	}

	@RequiresPermissions("mer:operationContentDetail:view")
	@RequestMapping(value = "form")
	public String form(OperationContentDetail operationContentDetail, Model model) {
		model.addAttribute("operationContentDetail", operationContentDetail);
		return "modules/mer/operationContentDetailForm";
	}

	@RequiresPermissions("mer:operationContentDetail:edit")
	@RequestMapping(value = "save")
	public String save(OperationContentDetail operationContentDetail, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, operationContentDetail)){
			return form(operationContentDetail, model);
		}
		operationContentDetailService.save(operationContentDetail);
		addMessage(redirectAttributes, "保存精选商品推荐成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationContentDetail/?repage";
	}
	
	@RequiresPermissions("mer:operationContentDetail:edit")
	@RequestMapping(value = "delete")
	public String delete(OperationContentDetail operationContentDetail, RedirectAttributes redirectAttributes) {
		operationContentDetailService.delete(operationContentDetail);
		addMessage(redirectAttributes, "删除精选商品推荐成功");
		return "redirect:"+Global.getAdminPath()+"/mer/operationContentDetail/?repage&opecontId="+operationContentDetail.getOpecontId()+"&opecontTitle="+operationContentDetail.getOpecontTitle();
	}
	
	
	@RequiresPermissions("mer:operationContentDetail:edit")
	@RequestMapping(value = "insertOperaContentDtl")
	@ResponseBody
	public Map<String,Object> insertOperaContentDtl(String id, String opecontId) {
		Map<String,Object> map = new HashMap<String,Object>();
		operationContentDetailService.insertOperaContentDtl(id,opecontId);
		map.put("msg", "添加成功");
		return map;
	}

}